﻿using UnityEngine;

public class PlayerPlatformerController : PhysicsObject {

    public float maxSpeed = 7;
    public float jumpTakeOffSpeed = 7;

    private Animator animator;

    void Awake () 
    {
        animator = GetComponent<Animator> ();
    }

    protected override void ComputeVelocity()
    {
        var move = Vector2.zero;

        move.x = Input.GetAxis ("Horizontal");

		if (Input.GetButtonDown ("Jump") && grounded) {
			velocity.y = jumpTakeOffSpeed;
		} else if (Input.GetButtonUp ("Jump")) 
		{
			if (velocity.y > 0) {
				velocity.y = velocity.y * 0.5f;
			}
		}

        if(move.x > 0.01f)
        {
            if(transform.rotation.eulerAngles.y != 0)
            {
                transform.rotation = Quaternion.Euler(0,0,0);
            }
        } 
        else if (move.x < -0.01f)
        {
            if(transform.rotation.eulerAngles.y == 0)
            {
                transform.rotation = Quaternion.Euler(0, 180, 0);
            }
        }

        animator.SetBool ("grounded", grounded);
        if (Input.GetKeyDown(KeyCode.S) && grounded)
        {
            animator.SetBool("isSlide",true);
        }
        if(Input.GetKeyUp(KeyCode.S) || !grounded)
        {
            animator.SetBool("isSlide", false);
        }
        animator.SetFloat ("velocityX", Mathf.Abs (velocity.x) / maxSpeed);

        targetVelocity = move * maxSpeed;
    }
}